package com.maoye.ossp.biz.brand.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.maoye.ossp.biz.brand.entity.BrandProxy;
import com.maoye.ossp.biz.brand.entity.SupplierBrand;
import com.maoye.ossp.biz.brand.service.IBrandProxyService;
import com.maoye.ossp.biz.brand.service.ISupplierBrandService;
import com.maoye.ossp.biz.brand.utils.BrandCommonConstant;
import com.maoye.ossp.biz.brand.utils.BrandUtils;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.org.util.UserUtils;
import com.maoye.sc.api.domain.User;
import com.maoye.sc.service.user.UserServiceImpl;

@Controller
@RequestMapping("/biz/brand/supplierBrand/")
public class SupplierBrandController extends BaseController
{
	@Resource
	private ISupplierBrandService supplierBrandService;
	@Resource
	private IBrandProxyService brandProxyService;
	@Resource
	public UserServiceImpl userServiceImpl;
	
	@RequestMapping("list")
	public String list(SupplierBrand supplierBrand, HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception
	{
		QueryFilter queryFilter = getQuerFilter(request, supplierBrand);
		User user = userServiceImpl.getUserByAccountID(UserUtils.getCurrentAccountId().toString());
		if(null != user && "SUPPLIER".equals(user.getUserType()))
		{
			queryFilter.getParams().put("supplierId", user.getId());
		}
		Page<SupplierBrand> page = supplierBrandService.find(queryFilter);
		model.addAttribute("page", page);
		return resultPage("/biz/brand/supplierBrand/list", model, request);
	}

	@RequestMapping("dialoglist")
	public String dialoglist(SupplierBrand supplierBrand, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		QueryFilter queryFilter = getQuerFilter(request, supplierBrand);
		Page<SupplierBrand> page = supplierBrandService.find(queryFilter);
		model.addAttribute("page", page);
		return resultPage("/biz/brand/supplierBrand/modalSupBrandList", model, request);
	}
	
	@RequestMapping(value = "save")
	public String save(SupplierBrand supplierBrand, Model model,
			RedirectAttributes redirectAttributes,HttpServletRequest request) 
	{
		String navId = request.getParameter("navId");
		String resultPage = resultPage("redirect:/biz/brand/supplierBrand/list?navId="+navId, model, request);
		try
		{
			String msg = supplierBrandService.updateOrAdd(supplierBrand);
			if(null != msg && !"".equals(msg))
			{
				addMessage(model, msg);
				model.addAttribute("supplierBrand", supplierBrand);
				resultPage = "/biz/brand/supplierBrand/add";
			}
		} 
		catch (Exception e) 
		{
			addMessage(model, BrandCommonConstant.EX_1009);
			resultPage = "/biz/brand/supplierBrand/add";
		}
		return resultPage;
	}

	@RequestMapping(value = "{id}/view")
	public String view(@PathVariable String id, Model model,
			RedirectAttributes redirectAttributes,HttpServletRequest request)
	{
		SupplierBrand supplierBrand = supplierBrandService.getById(Long.valueOf(id));
		String navId = request.getParameter("navId");
		model.addAttribute("navId", navId);
		model.addAttribute("supplierBrand", supplierBrand);
		return "/biz/brand/supplierBrand/view";
	}
	
	@RequestMapping(value = "{id}/edit")
	public String edit(@PathVariable String id, Model model,
			RedirectAttributes redirectAttributes,HttpServletRequest request) 
	{
		SupplierBrand supplierBrand = supplierBrandService.getById(Long.valueOf(id));
		User user = userServiceImpl.getUserById(UserUtils.getCurrentUserId().toString());
		String navId = request.getParameter("navId");
		model.addAttribute("navId", navId);
		setDictToModel(model);
		model.addAttribute("currUserRole", user.getUserType());
		model.addAttribute("supplierBrand", supplierBrand);
		return "/biz/brand/supplierBrand/add";
	}
	
	@RequestMapping(value = "add")
	public String add(SupplierBrand supplierBrand,Model model,HttpServletRequest request)
	{
		User user = userServiceImpl.getUserByAccountID(UserUtils.getCurrentAccountId().toString());
		if(null != user && "SUPPLIER".equals(user.getUserType()))
		{
			supplierBrand.setSupplierId(user.getId());
		}
		String navId = request.getParameter("navId");
		model.addAttribute("navId", navId);
		model.addAttribute("currUserRole", user.getUserType());
		setDictToModel(model);
		model.addAttribute("supplierBrand", supplierBrand);
		return "/biz/brand/supplierBrand/add"; 
	}
	
	@RequestMapping(value = "{id}/delete")
	public String delete(@PathVariable String id, Model model,
			RedirectAttributes redirectAttributes,HttpServletRequest request)
	{
		supplierBrandService.delByPK(Long.valueOf(id));
		String navId = request.getParameter("navId");
		addMessage(redirectAttributes, "删除数据成功！");
		return "redirect:/biz/brand/supplierBrand/list?navId="+navId;
	}
	
	@RequestMapping(value = "deleteAll")
	public String deleteAll(HttpServletRequest request,
			HttpServletResponse response, Model model,RedirectAttributes redirectAttributes) 
	{
		String deleteIds = request.getParameter("deleteIds");
		Long[] lAryId = BrandUtils.getLongAryByStr(deleteIds,";");
		supplierBrandService.delByPK(lAryId);
		String navId = request.getParameter("navId");
		addMessage(redirectAttributes, "删除数据成功！");
		return "redirect:/biz/brand/supplierBrand/list?navId="+navId;
	}
	
	public void setDictToModel(Model model){
		List<BrandProxy> brandProxyList=brandProxyService.getAll();
		model.addAttribute("brandProxyList", brandProxyList);
	}
	
	@RequestMapping(value = "{id}/getShopList")
	public void getShopList(@PathVariable String id,Model model, HttpServletRequest request,
			HttpServletResponse response) throws Exception 
	{
		String jsonStr = supplierBrandService.getShopList(id);
        response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();
		out.write(jsonStr);
	}
}
